jun.KonsumenWin = Ext.extend(Ext.Window, {
    title: 'Master Konsumen',
    modez: 1,
    width: 400,
    height: 440,
    layout: 'form',
    modal: true,
    padding: 5,
    closeForm: false,
    initComponent: function () {
        this.items = [
            {
                xtype: 'form',
                frame: false,
                bodyStyle: 'background-color: #E4E4E4; padding: 10px',
                id: 'form-Konsumen',
                labelWidth: 100,
                labelAlign: 'left',
                layout: 'form',
                ref: 'formz',
                border: false,
                items: [
                    {
                        xtype: 'uctextfield',
                        fieldLabel: 'Kode Konsumen',
                        hideLabel: false,
                        //hidden:true,
                        name: 'konsumen_code',
                        id: 'konsumen_codeid',
                        ref: '../konsumen_code',
                        maxLength: 20,
                        //allowBlank: 1,
                        anchor: '100%'
                    },
                    {
                        xtype: 'uctextfield',
                        fieldLabel: 'Nama Konsumen',
                        hideLabel: false,
                        //hidden:true,
                        name: 'konsumen_name',
                        id: 'konsumen_nameid',
                        ref: '../konsumen_name',
                        maxLength: 100,
                        //allowBlank: 1,
                        anchor: '100%'
                    },
                    {
                        xtype: 'combo',
                        typeAhead: true,
                        triggerAction: 'all',
                        lazyRender: true,
                        mode: 'local',
                        fieldLabel: 'Tipe Konsumen',
                        store: jun.rztTypeKonsumen,
                        hiddenName: 'type_konsumen_id',
                        forceSelection: true,
                        valueField: 'type_konsumen_id',
                        ref: '../type_konsumen_id',
                        displayField: 'type_konsumen_name',
                        //allowBlank:false,
                        anchor: '100%'
                    },
                    {
                        xtype: 'combo',
                        typeAhead: true,
                        triggerAction: 'all',
                        lazyRender: true,
                        mode: 'local',
                        fieldLabel: 'Pasar',
                        store: jun.rztPasar,
                        hiddenName: 'pasar_id',
                        valueField: 'pasar_id',
                        ref: '../pasar_id',
                        displayField: 'pasar_name',
                        //allowBlank:false,
                        anchor: '100%'
                    },
                    {
                        xtype: 'combo',
                        typeAhead: true,
                        triggerAction: 'all',
                        lazyRender: true,
                        mode: 'local',
                        fieldLabel: 'Area',
                        store: jun.rztArea,
                        hiddenName: 'area_id',
                        forceSelection: true,
                        valueField: 'area_id',
                        ref: '../area_id',
                        displayField: 'area_name',
                        //allowBlank:false,
                        anchor: '100%'
                    },
                    {
                        xtype: 'uctextfield',
                        fieldLabel: 'Phone',
                        hideLabel: false,
                        //hidden:true,
                        name: 'phone',
                        id: 'phoneid',
                        ref: '../phone',
                        maxLength: 25,
                        //allowBlank: 1,
                        anchor: '100%'
                    },
                    {
                        xtype: 'uctextfield',
                        fieldLabel: 'HP',
                        hideLabel: false,
                        //hidden:true,
                        name: 'hp',
                        id: 'hpid',
                        ref: '../hp',
                        maxLength: 25,
                        //allowBlank: 1,
                        anchor: '100%'
                    },
                    {
                        xtype: 'uctextfield',
                        fieldLabel: 'HP 2',
                        hideLabel: false,
                        //hidden:true,
                        name: 'hp2',
                        id: 'hp2id',
                        ref: '../hp2',
                        maxLength: 25,
                        //allowBlank: 1,
                        anchor: '100%'
                    },
                    {
                        xtype: 'numericfield',
                        fieldLabel: 'Tempo',
                        hideLabel: false,
                        value: 0,
                        name: 'tempo',
                        id: 'tempoid',
                        ref: '../tempo',
                        maxLength: 6,
                        //allowBlank: 1,
                        anchor: '100%'
                    },
                    {
                        xtype: 'textarea',
                        fieldLabel: 'Alamat',
                        hideLabel: false,
                        //hidden:true,
                        name: 'address',
                        id: 'addressid',
                        ref: '../address',
                        maxLength: 100,
                        //allowBlank: 1,
                        anchor: '100%'
                    },
                    {
                        xtype: 'checkbox',
                        fieldLabel: 'Agen',
                        hideLabel: false,
                        value: 1,
                        inputValue: 1,
                        uncheckedValue: 0,
                        name: 'agen',
                        id: 'agenid',
                        ref: '../agen'
                    },
                    new jun.comboActive({fieldLabel: "Status", hideLabel: !1, width: 100, height: 20, name: "status", id: "statusid", ref: "../status", hiddenName: "status"})
                ]
            }
        ];
        this.fbar = {
            xtype: 'toolbar',
            items: [
                {
                    xtype: 'button',
                    text: 'Simpan',
                    hidden: false,
                    ref: '../btnSave'
                },
                {
                    xtype: 'button',
                    text: 'Simpan & Tutup',
                    ref: '../btnSaveClose'
                },
                {
                    xtype: 'button',
                    text: 'Batal',
                    ref: '../btnCancel'
                }
            ]
        };
        jun.KonsumenWin.superclass.initComponent.call(this);
//        this.on('activate', this.onActivate, this);
        this.btnSaveClose.on('click', this.onbtnSaveCloseClick, this);
        this.btnSave.on('click', this.onbtnSaveclick, this);
        this.btnCancel.on('click', this.onbtnCancelclick, this);
        this.type_konsumen_id.on('blur', this.autoSetPasar, this);
        this.type_konsumen_id.on('expand', this.onExpandKonsumen, this);
        this.area_id.on('expand', this.onExpandArea, this);
        this.pasar_id.on('expand', this.onExpandPasar, this);
        this.on("close", this.onWinClose, this)
        if (this.modez == 1 || this.modez == 2) {
            this.btnSave.setVisible(false);
        } else {
            this.btnSave.setVisible(true);
        }
    },
    onExpandKonsumen: function () {
        jun.rztTypeKonsumen.FilterData();
    },
    onExpandPasar: function () {
        jun.rztPasar.FilterData();
    },
    onExpandArea: function () {
        jun.rztArea.FilterData();
    },
    btnDisabled: function (status) {
        this.btnSave.setDisabled(status);
        this.btnSaveClose.setDisabled(status);
    },
    onWinClose: function () {
        jun.rztKonsumenMaster.clearFilter(),
            jun.rztPasar.clearFilter(),
            jun.rztArea.clearFilter()
    },
    autoSetPasar: function () {
        if (this.type_konsumen_id.getValue() == "1") {
            this.pasar_id.setDisabled(false);
        }
        else {
            this.pasar_id.setDisabled(true);
        }
    },
    saveForm: function () {
        this.btnDisabled(true);
        var urlz;
        if (this.modez == 1 || this.modez == 2) {
            urlz = 'Konsumen/update/id/' + this.id;
        } else {
            urlz = 'Konsumen/create/';
        }
        Ext.getCmp('form-Konsumen').getForm().submit({
            url: urlz,
            timeOut: 1000,
            scope: this,
            success: function (f, a) {
                jun.rztKonsumen.reload();
                jun.rztKonsumenLib.reload();
                jun.rztKonsumenMaster.reload();
                var response = Ext.decode(a.response.responseText);
                Ext.MessageBox.show({
                    title: 'Info',
                    msg: response.msg,
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.INFO
                });
                if (this.modez == 0) {
                    Ext.getCmp('form-Konsumen').getForm().reset();
                    this.btnDisabled(false);
                }
                if (this.closeForm) {
                    this.close();
                }
            },
            failure: function (f, a) {
                switch (a.failureType) {
                    case Ext.form.Action.CLIENT_INVALID:
                        Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                        break;
                    case Ext.form.Action.CONNECT_FAILURE:
                        Ext.Msg.alert(a.response.statusText, a.response.responseText);
                        break;
                    case Ext.form.Action.SERVER_INVALID:
                        Ext.Msg.alert('Failure', a.result.msg);
                }
                this.btnDisabled(false);
            }

        });
    },
    onbtnSaveCloseClick: function () {
        this.closeForm = true;
        this.saveForm(true);
    },
    onbtnSaveclick: function () {
        this.closeForm = false;
        this.saveForm(false);
    },
    onbtnCancelclick: function () {
        this.close();
    }
});